import { Injectable } from '@angular/core';
import {RuleExpression} from "../model/rule-expression";
import {ExecActionMainService} from "../plugin/exec-action-main.service";
import {FileInfo} from "../model/file-info";
import {SystemUtilsService} from "./system-utils.service";

@Injectable({
  providedIn: 'root'
})
export class RuleExecService {

  constructor(protected execActionMain:ExecActionMainService ,protected system:SystemUtilsService) {

  }

  public createFileInfo(path:string){
    return new FileInfo(
      this.system.getFileName(path),
      path,
      this.system.getFileDir(path),
      this.system.getFileExt(path)
      )
  }
  public async execRule(file:FileInfo,lastVal?:any,rules?:RuleExpression[],execOne?:boolean){
    if (rules == null){
      return;
    }
    for (let rule of rules) {
        let execAction = this.execActionMain.getExecAction(rule.action);
        if (execAction != null){
          let exec = await execAction.exec(file,lastVal,rule.expression);
          this.system.debug(`执行 ${rule.action}  => `,exec)
           if (exec){
             let nextExecOne = rule.action=='execOne'
            let rs =  await this.execRule(file,exec,rule.children,nextExecOne)
             if (execOne){
               this.system.info("[execOne]:终止下一条执行")
             }
          }
        }else{
          this.system.info("无法识别操作=>",rule.action)
        }
    }
  }
}
